﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;
using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string ProductSaleBack_M_Add(ProductSaleBack_M model, int ACID);


        [OperationContract]
        string ProductSaleBack_M_Update(ProductSaleBack_M model, int ACID);


        [OperationContract]
        string ProductSaleBack_MBydjn_Delete(string djn, int ACID);


        [OperationContract]
        ProductSaleBack_M ProductSaleBack_MBydjn_Get(string djn, int ACID);


        [OperationContract]
        List<ProductSaleBack_M> ProductSaleBack_M_Get(string sortBy, int ACID);




    }
    public partial class Service : IService
    {
        #region  添加ProductSaleBack_M表数据
        /// <summary>
        /// 添加ProductSaleBack_M
        /// </summary>
        /// <param name="model">ProductSaleBack_M实体</param>
        /// <returns>空或错误</returns>
        public string ProductSaleBack_M_Add(ProductSaleBack_M model, int ACID)
        {
            if (string.IsNullOrEmpty(model.djn))
            {
                return "『单据号』不能为空！";
            }
            if (model.clientID == null || model.clientID.Equals(Guid.Empty))
            {
                return "『客户』不能为空！";
            }
            if (model.warehouseID == null || model.warehouseID.Equals(Guid.Empty))
            {
                return "『仓库』不能为空！";
            }
            if (model.odate == null)
            {
                return "『日期』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductSaleBack_M.Count(p => p.djn == model.djn) > 0)
                    {
                        return "主键已存在！";
                    }
                    ctx.ProductSaleBack_M.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  添加ProductSaleBack_M表数据事物用重构
        /// <summary>
        /// 添加ProductSaleBack_M事物用重构
        /// </summary>
        /// <param name="model">ProductSaleBack_M实体</param>
        /// <returns>空或错误</returns>
        private string ProductSaleBack_M_Add(ProductSaleBack_M model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(model.djn))
            {
                return "『单据号』不能为空！";
            }
            if (model.clientID == null || model.clientID.Equals(Guid.Empty))
            {
                return "『客户』不能为空！";
            }
            if (model.warehouseID == null || model.warehouseID.Equals(Guid.Empty))
            {
                return "『仓库』不能为空！";
            }
            if (model.odate == null)
            {
                return "『日期』不能为空！";
            }
            if (ctx.ProductSaleBack_M.Count(p => p.djn == model.djn) > 0)
            {
                return "主键已存在！";
            }
            ctx.ProductSaleBack_M.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  修改ProductSaleBack_M表数据
        /// <summary>
        /// 修改ProductSaleBack_M
        /// </summary>
        /// <param name="model">ProductSaleBack_M实体</param>
        /// <returns>空或错误</returns>
        public string ProductSaleBack_M_Update(ProductSaleBack_M model, int ACID)
        {
            if (model.clientID == null || model.clientID.Equals(Guid.Empty))
            {
                return "『客户』不能为空！";
            }
            if (model.warehouseID == null || model.warehouseID.Equals(Guid.Empty))
            {
                return "『仓库』不能为空！";
            }
            if (model.odate == null)
            {
                return "『日期』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductSaleBack_M.Count(p => p.djn == model.djn) != 1)
                    {
                        return "未找到指定记录或记录不唯一！";
                    }
                    ProductSaleBack_M ProductSaleBack_Mmodel = ctx.ProductSaleBack_M.Single(p => p.djn == model.djn);
                    if (!string.IsNullOrEmpty(model.djn))
                    {
                        ProductSaleBack_Mmodel.djn = model.djn;
                    }
                    if (model.odate != null)
                    {
                        ProductSaleBack_Mmodel.odate = model.odate;
                    }
                    if (!string.IsNullOrEmpty(model.warehouseID))
                    {
                        ProductSaleBack_Mmodel.warehouseID = model.warehouseID;
                    }
                    if (model.clientID != null || model.clientID.Equals(Guid.Empty))
                    {
                        ProductSaleBack_Mmodel.clientID = model.clientID;
                    }
                    if (!string.IsNullOrEmpty(model.linkman))
                    {
                        ProductSaleBack_Mmodel.linkman = model.linkman;
                    }
                    if (!string.IsNullOrEmpty(model.linktel))
                    {
                        ProductSaleBack_Mmodel.linktel = model.linktel;
                    }
                    if (!string.IsNullOrEmpty(model.linkaddress))
                    {
                        ProductSaleBack_Mmodel.linkaddress = model.linkaddress;
                    }
                    if (!string.IsNullOrEmpty(model.srpznum))
                    {
                        ProductSaleBack_Mmodel.srpznum = model.srpznum;
                    }
                    if (model.total != null)
                    {
                        ProductSaleBack_Mmodel.total = model.total;
                    }
                    if (model.banlance != null)
                    {
                        ProductSaleBack_Mmodel.banlance = model.banlance;
                    }
                    if (model.discount != null)
                    {
                        ProductSaleBack_Mmodel.discount = model.discount;
                    }
                    if (model.jsuser != null || model.jsuser.Equals(Guid.Empty))
                    {
                        ProductSaleBack_Mmodel.jsuser = model.jsuser;
                    }
                    //if (model.ischeck != null)
                    //{
                    //    ProductSaleBack_Mmodel.ischeck = model.ischeck;
                    //}
                    //if (model.Checkemdjn != null || model.Checkemdjn.Equals(Guid.Empty))
                    //{
                    //    ProductSaleBack_Mmodel.Checkemdjn = model.Checkemdjn;
                    //}
                    //if (model.isfinish != null)
                    //{
                    //    ProductSaleBack_Mmodel.isfinish = model.isfinish;
                    //}
                    if (!string.IsNullOrEmpty(model.remarks))
                    {
                        ProductSaleBack_Mmodel.remarks = model.remarks;
                    }
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  修改ProductSaleBack_M表数据--事物重构
        /// <summary>
        /// 修改ProductSaleBack_M
        /// </summary>
        /// <param name="model">ProductSaleBack_M实体</param>
        /// <returns>空或错误</returns>
        public string ProductSaleBack_M_Update(ProductSaleBack_M model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (model.clientID == null || model.clientID.Equals(Guid.Empty))
            {
                return "『客户』不能为空！";
            }
            if (model.warehouseID == null || model.warehouseID.Equals(Guid.Empty))
            {
                return "『仓库』不能为空！";
            }
            if (model.odate == null)
            {
                return "『日期』不能为空！";
            }

            try
            {
                if (ctx.ProductSaleBack_M.Count(p => p.djn == model.djn) != 1)
                {
                    return "未找到指定记录或记录不唯一！";
                }
                ProductSaleBack_M ProductSaleBack_Mmodel = ctx.ProductSaleBack_M.Single(p => p.djn == model.djn);
                if (!string.IsNullOrEmpty(model.djn))
                {
                    ProductSaleBack_Mmodel.djn = model.djn;
                }
                if (model.odate != null)
                {
                    ProductSaleBack_Mmodel.odate = model.odate;
                }
                if (!string.IsNullOrEmpty(model.warehouseID))
                {
                    ProductSaleBack_Mmodel.warehouseID = model.warehouseID;
                }
                if (model.clientID != null || model.clientID.Equals(Guid.Empty))
                {
                    ProductSaleBack_Mmodel.clientID = model.clientID;
                }
                if (!string.IsNullOrEmpty(model.linkman))
                {
                    ProductSaleBack_Mmodel.linkman = model.linkman;
                }
                if (!string.IsNullOrEmpty(model.linktel))
                {
                    ProductSaleBack_Mmodel.linktel = model.linktel;
                }
                if (!string.IsNullOrEmpty(model.linkaddress))
                {
                    ProductSaleBack_Mmodel.linkaddress = model.linkaddress;
                }
                if (!string.IsNullOrEmpty(model.srpznum))
                {
                    ProductSaleBack_Mmodel.srpznum = model.srpznum;
                }
                if (model.total != null)
                {
                    ProductSaleBack_Mmodel.total = model.total;
                }
                if (model.banlance != null)
                {
                    ProductSaleBack_Mmodel.banlance = model.banlance;
                }
                if (model.discount != null)
                {
                    ProductSaleBack_Mmodel.discount = model.discount;
                }
                if (model.jsuser != null || model.jsuser.Equals(Guid.Empty))
                {
                    ProductSaleBack_Mmodel.jsuser = model.jsuser;
                }
                if (!string.IsNullOrEmpty(model.remarks))
                {
                    ProductSaleBack_Mmodel.remarks = model.remarks;
                }
                return string.Empty;
            }
            catch (Exception ex)
            {
                return " 修改失败!错误详细信息：" + ex.Message;
            }

        }
        #endregion


        #region  删除ProductSaleBack_M表Bydjn数据
        /// <summary>
        /// 删除ProductSaleBack_MBydjn
        /// </summary>
        /// <param name="djn">单据号</param>
        /// <returns>空或错误</returns>
        public string ProductSaleBack_MBydjn_Delete(string djn, int ACID)
        {
            if (string.IsNullOrEmpty(djn))
            {
                return "『单据号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductSaleBack_M.Count(p => p.djn == djn) != 1)
                    {
                        return null;
                    }
                    ProductSaleBack_M model = ctx.ProductSaleBack_M.Single(p => p.djn == djn);
                    ctx.ProductSaleBack_M.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 删除失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  删除ProductSaleBack_M表Bydjn数据[事物用重构]
        /// <summary>
        /// 删除ProductSaleBack_MBydjn[事物用重构]
        /// </summary>
        /// <param name="djn">单据号</param>
        /// <returns>空或错误</returns>
        private string ProductSaleBack_MBydjn_Delete(string djn, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(djn))
            {
                return "『单据号』不能为空！";
            }
            if (ctx.ProductSaleBack_M.Count(p => p.djn == djn) != 1)
            {
                return null;
            }
            ProductSaleBack_M model = ctx.ProductSaleBack_M.Single(p => p.djn == djn);
            ctx.ProductSaleBack_M.DeleteOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  获取ProductSaleBack_M表Bydjn数据
        /// <summary>
        /// 获取ProductSaleBack_MBydjn
        /// </summary>
        /// <param name="djn">单据号</param>
        /// <returns>当前表</returns>
        public ProductSaleBack_M ProductSaleBack_MBydjn_Get(string djn, int ACID)
        {
            if (string.IsNullOrEmpty(djn))
            {
                return null;
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductSaleBack_M.Count(p => p.djn == djn) != 1)
                    {
                        return null;
                    }
                    return ctx.ProductSaleBack_M.SingleOrDefault(p => p.djn == djn);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion



        #region  获取ProductSaleBack_M表数据
        /// <summary>
        /// 获取ProductSaleBack_M
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<ProductSaleBack_M></returns>
        public List<ProductSaleBack_M> ProductSaleBack_M_Get(Expression<Func<ProductSaleBack_M, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.ProductSaleBack_M.ToList();
                        }
                        else
                        {
                            return ctx.ProductSaleBack_M.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.ProductSaleBack_M.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.ProductSaleBack_M.Where(predicate).OrderBy(sortBy).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取ProductSaleBack_M表数据
        /// <summary>
        /// 获取ProductSaleBack_M
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<ProductSaleBack_M></returns>
        public List<ProductSaleBack_M> ProductSaleBack_M_Get(string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    return ProductSaleBack_M_Get(null, sortBy, ACID);
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取ProductSaleBack_M表数据计数
        /// <summary>
        /// 获取ProductSaleBack_M表数据计数
        /// </summary>
        /// <returns>List<ProductSaleBack_M></returns>
        public int ProductSaleBack_M_GetCount(Expression<Func<ProductSaleBack_M, bool>> predicate, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        return ctx.ProductSaleBack_M.Count();

                    }
                    else
                    {
                        return ctx.ProductSaleBack_M.Where(predicate).Count();
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion




        #region  获取ProductSaleBack_M分页表数据
        /// <summary>
        /// 获取ProductSaleBack_M分页表数据
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<ProductSaleBack_M></returns>
        public List<ProductSaleBack_M> ProductSaleBack_M_Getpage(int pageIndex, int pageSize, Expression<Func<ProductSaleBack_M, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.ProductSaleBack_M.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.ProductSaleBack_M.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.ProductSaleBack_M.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.ProductSaleBack_M.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  批量修改ProductSaleBack_M表数据
        /// <summary>
        /// 批量修改ProductSaleBack_M
        /// </summary>
        /// <param name="predicate">Expression<Func<ProductSaleBack_M, bool>></param> 查找表达式树
        /// <param name="uodater">Expression<Func<ProductSaleBack_M,ProductSaleBack_M>> 更新表达式树</param>
        /// <param name="returns">影响记录数</param>
        public int ProductSaleBack_M_batchUodate(Expression<Func<ProductSaleBack_M, bool>> predicate, Expression<Func<ProductSaleBack_M, ProductSaleBack_M>> uodater, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.ProductSaleBack_M.Update(predicate, uodater);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量修改ProductSaleBack_M表数据事物用重构
        /// <summary>
        /// 批量修改ProductSaleBack_M事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<ProductSaleBack_M, bool>></param> 查找表达式树
        /// <param name="uodater">Expression<Func<ProductSaleBack_M,ProductSaleBack_M>> 更新表达式树</param>
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int ProductSaleBack_M_batchUodate(Expression<Func<ProductSaleBack_M, bool>> predicate, Expression<Func<ProductSaleBack_M, ProductSaleBack_M>> uodater, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.ProductSaleBack_M.Update(predicate, uodater);
            return ok;
        }
        #endregion


        #region  批量删除ProductSaleBack_M表数据
        /// <summary>
        /// 批量删除ProductSaleBack_M
        /// </summary>
        /// <param name="predicate">Expression<Func<ProductSaleBack_M, bool>></param> 查找表达式树
        /// <param name="returns">影响记录数</param>
        public int ProductSaleBack_M_batchDel(Expression<Func<ProductSaleBack_M, bool>> predicate, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.ProductSaleBack_M.Delete(predicate);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量删除ProductSaleBack_M表数据事物用重构
        /// <summary>
        /// 批量删除ProductSaleBack_M事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<ProductSaleBack_M, bool>></param> 查找表达式树
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int ProductSaleBack_M_batchDel(Expression<Func<ProductSaleBack_M, bool>> predicate, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.ProductSaleBack_M.Delete(predicate);
            return ok;
        }
        #endregion


    }
}
